package ipgather.zdy;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import ipgather.core.redis.RedisDbAdapter;
import redis.clients.jedis.Jedis;

public class ZDYCode {

	public static void main(String[] args) {
		boolean callback = true;
		while(true){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if(callback){
				callback = false;
				CloseableHttpClient httpClient = HttpClients.createDefault();
				HttpPost request = new HttpPost("http://check.zdaye.com/Service.asmx");
				request.addHeader("Content-Type", "text/xml; charset=utf-8");
				request.addHeader("SOAPAction", "http://tempuri.org/SaoMiaoGetIPSource");
				try {
					request.setEntity(new StringEntity("<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><SaoMiaoGetIPSource xmlns=\"http://tempuri.org/\"><username>lake201314</username><password>lailake</password><v>8D385C44E0285206921CBF4E9F4AB7CF</v><m>64:31:50:18:97:B4|00:50:56:C0:00:01|00:50:56:C0:00:08</m></SaoMiaoGetIPSource></soap:Body></soap:Envelope>"));
					HttpResponse response = httpClient.execute(request);
					String content = EntityUtils.toString(response.getEntity(),Consts.UTF_8);
					content = content.substring(content.indexOf("<result>")+17,content.lastIndexOf("</result>")+5);
					String[] hostAndPort = content.split("@@");
					System.out.println("读取到ip:"+hostAndPort.length);
					if(hostAndPort.length==1){
						int sTime = (new Random().nextInt(60)+10);
						System.err.println("读取IP失败,休眠"+sTime+"秒");
						try {
							Thread.sleep(sTime*1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}else{
						RedisDbAdapter redisDbAdapter = new RedisDbAdapter();
						Jedis jedis = redisDbAdapter.getJedis();
						for(String hostPort : hostAndPort){
							jedis.set(hostPort, hostPort);
						}
						jedis.close();
						System.out.println("成功保存:"+hostAndPort.length);
					}
					callback = true;
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					try {
						httpClient.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		}
	}
}
